package cn.herlioner.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import cn.herlioner.dao.Obj_StuDAO;
import cn.herlioner.dao.impl.Obj_StuDAOImpl;
import cn.herlioner.dao.impl.Obj_TeaDAOImpl;
import cn.herlioner.dao.impl.ObjectDAOImpl;
import cn.herlioner.dao.impl.StudentDAOImpl;
import cn.herlioner.pojo.CRUObject;
import cn.herlioner.pojo.Obj_Stu;
import cn.herlioner.pojo.Obj_Tea;
import cn.herlioner.pojo.Student;
import cn.herlioner.pojo.TSObject;
import cn.herlioner.service.CRUObjectService;
import cn.herlioner.service.SelectobjService;
import cn.herlioner.service.SelectobjServiceImpl;


public class Obj_StuAction extends ActionSupport implements SessionAware {
	Obj_Stu obj_Stu;
	Obj_StuDAOImpl obj_StuDAO = new Obj_StuDAOImpl();
	ObjectDAOImpl objectDAO = new ObjectDAOImpl();
	private Long[] objselect = new Long[]{};
	private int[] bookselect = new int[] {};
	private int redi;
	private String objnameorteanameselect;
	private CRUObject cruObject;
	
	public String selectobj(){
		Map session = ActionContext.getContext().getSession();
		HttpServletRequest request = ServletActionContext.getRequest();
//		//根据课程名称或老师名称查询
//		if(this.objnameorteanameselect!=null){
//			System.out.println("为什么啊！！！！！");
//			session.put("objnameorteanameselect", this.objnameorteanameselect);
//			return "objnameorteanameselect";
//		}
//		//根据学分查询
//		if(this.redi !=0){
//			switch (this.redi) {
//			case 1:
//				session.put("redi", 1);
//				return "getbyredi"; 
//			case 2:
//				session.put("redi", 2);
//				return "getbyredi"; 
//			case 3:
//				session.put("redi", 3);	
//				return "getbyredi"; 
//			case 4:
//				session.put("redi", 4);
//				return "getbyredi"; 
//			default:
//				break;
//			}
//		}
//		
		//获取登陆的学生账号信息
		long stunum = (long)session.get("Num");
		SelectobjServiceImpl selectobjServiceImpl = new SelectobjServiceImpl();
		int listobj = this.objselect.length;//选择的所有课程的数组长度
		System.out.println(listobj);
		if(listobj == 0){
			return "objnull";
		}
		
		CRUObjectService  cruObjectService = new CRUObjectService();
		List<CRUObject> cruObjects = new ArrayList<CRUObject>();
		for (int i=0;i<listobj;i++) {
			System.out.println(objselect[i]);
			CRUObject cruObject = new CRUObject();
			cruObjects.add(cruObjectService.setobjselct(objselect[i],cruObject));
		}
		request.setAttribute("cruObjects", cruObjects);
		return "success";
		}
		
	//储存提交信息
	public String selectupdate(){
		
		Map session = ActionContext.getContext().getSession();
		
		HttpServletRequest request = ServletActionContext.getRequest();
		
		//获取登陆的学生账号信息
				long stunum = (long)session.get("Num");
				SelectobjServiceImpl selectobjServiceImpl = new SelectobjServiceImpl();
				int listobj = this.objselect.length;//选择的所有课程的数组长度
				System.out.println(listobj);
				if(listobj == 0){
					return "objnull";
				}
				
				CRUObjectService  cruObjectService = new CRUObjectService();
				List<CRUObject> cruObjects = new ArrayList<CRUObject>();
				
				for (int i=0;i<listobj;i++) {
					//储存信息
					//1,改变tsobject信息
					long objnum = objselect[i];
					TSObject tso = objectDAO.get(objnum).get(0);
					tso.setAllowance(tso.getAllowance()-1);
					objectDAO.update(tso);
					//2，添加obj_stu信息
					Obj_Stu obj_Stu = new Obj_Stu();
					obj_Stu.setBook((long)0);
					obj_Stu.setObjnum(objnum);
					obj_Stu.setStunum(stunum);
					obj_Stu.setId((obj_StuDAO.getMaxID()+1));
					obj_StuDAO.add(obj_Stu);
				}
		return "success";
	}
	//
	public String obj_stuSearch(){
		HttpServletRequest request = ServletActionContext.getRequest();
		Long objnum = cruObject.getObjnum();
		System.out.println(objnum);
		List<Obj_Stu> ot = obj_StuDAO.getbyobjnum(objnum);
		
		List<CRUObject> cruObjects;
		List<Student> students = new ArrayList();
		try{
			for (int i = 0 ;i<ot.size();i++) {
				ot.get(i).toString();
		    	Student student = new StudentDAOImpl().get(ot.get(i).getStunum());
		    	students.add(student);
				}
			}catch (Exception e) {
				// TODO: handle exception
			}
		request.setAttribute("students", students);
		return "getsuccess";
	}
	
	
	
	public CRUObject getCruObject() {
		return cruObject;
	}


	public void setCruObject(CRUObject cruObject) {
		this.cruObject = cruObject;
	}


	public String getObjnameorteanameselect() {
		return objnameorteanameselect;
	}

	public void setObjnameorteanameselect(String objnameorteanameselect) {
		this.objnameorteanameselect = objnameorteanameselect;
	}

	public int getRedi() {
		return redi;
	}

	public void setRedi(int redi) {
		this.redi = redi;
	}

	public Long[] getObjselect() {
		return objselect;
	}

	public void setObjselect(Long[] objselect) {
		this.objselect = objselect;
	}



	public int[] getBookselect() {
		return bookselect;
	}

	public void setBookselect(int[] bookselect) {
		this.bookselect = bookselect;
	}

	public Obj_Stu getObj_Stu() {
		return obj_Stu;
	}

	public void setObj_Stu(Obj_Stu obj_Stu) {
		this.obj_Stu = obj_Stu;
	}

	public Obj_StuDAOImpl getObj_StuDAO() {
		return obj_StuDAO;
	}

	public void setObj_StuDAO(Obj_StuDAOImpl obj_StuDAO) {
		this.obj_StuDAO = obj_StuDAO;
	}
	
	


	@Override
	public void setSession(Map<String, Object> arg0) {
		// TODO Auto-generated method stub
		
	}

}
